package com.gxuwz.medical.domain.medical;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DBUtil;
import com.gxuwz.medical.exception.MedicalNotFoundException;

import java.sql.PreparedStatement;

/**
 * 医疗机构模块类
 * @author 沉羽墨玖
 *
 */
public class Medical {
	 // 机构编码
	private String instCode;
	 // 组织机构编码
	private String orgaCode;
	// 机构名称
	private String instName; 
	// 地区编码
	private String regCode; 
	// 行政区域编码
	private String areaCode; 
	// 隶属关系
	private Data subjection; 
	// 机构级别
	private Data instGrade; 
	// 申报定点类型
	private Data sbdd; 
	 // 批准定点类型
	private Data approve;
	 // 所属经济类型
	private Data economy;
	 // 卫生机构大类
	private Data hygieneBig;
	 // 卫生机构小类
	private Data hygieneSmall;
	// 主管单位
	private String unit; 
	// 开业时间
	private String openDate; 
	// 法人代表
	private String represent; 
	// 注册资金
	private double funds; 

	public Medical() {

	}

	public Medical(String instCode) throws MedicalNotFoundException {
		load(instCode);

	}

	// 全参构造函数
	public Medical(String instCode, String orgaCode, String instName,
			String regCode, String areaCode, Data subjection, Data instGrade,
			Data sbdd, Data approve, Data economy, Data hygieneBig,
			Data hygieneSmall, String unit, String openDate, String represent,
			double funds) {
		super();
		this.instCode = instCode;
		this.orgaCode = orgaCode;
		this.instName = instName;
		this.regCode = regCode;
		this.areaCode = areaCode;
		this.subjection = subjection;
		this.instGrade = instGrade;
		this.sbdd = sbdd;
		this.approve = approve;
		this.economy = economy;
		this.hygieneBig = hygieneBig;
		this.hygieneSmall = hygieneSmall;
		this.unit = unit;
		this.openDate = openDate;
		this.represent = represent;
		this.funds = funds;
	}

	private void load(String instCode) throws MedicalNotFoundException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from medical where instCode=?");
			int index = 1;
			pstmt.setString(index, instCode);
			rs = pstmt.executeQuery();
			if (rs != null && rs.next()) {
				this.orgaCode = rs.getString("orgaCode");
				this.instName = rs.getString("instName");
				this.regCode = rs.getString("regCode");
				this.areaCode = rs.getString("areaCode");
				String subjection = rs.getString("subjection");
				String instGrade = rs.getString("instGrade");
				String sbdd = rs.getString("sbdd");
				String economy = rs.getString("economy");
				String approve = rs.getString("approve");
				String hygieneBig = rs.getString("hygieneBig");
				String hygieneSmall = rs.getString("hygieneSmall");
				this.unit = rs.getString("unit");
				this.openDate = rs.getString("openDate");
				this.represent = rs.getString("represent");
				String funds = rs.getString("funds");
				this.instCode = rs.getString("instCode");
			}
		} catch (SQLException e) {
			throw new MedicalNotFoundException("Medical with id " + instCode
					+ " could not be loaded from the database.");
		} finally {
			DBUtil.close(rs, pstmt, conn);
		}

	}

	public void add() throws SQLException {
		Connection conn = null;
		try {
			// 1:获得连接
			conn = DBUtil.getConn();
			// 2;保存到数据库
			conn.setAutoCommit(false);
			saveToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DBUtil.close(conn);
		}
	}

	private void saveToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into medical");
			sqlBuff.append("(instCode,orgaCode,instName,regCode,areaCode,subjection,instGrade,sbdd,approve,economy,hygieneBig,hygieneSmall,unit,openDate,represent,funds)");
			sqlBuff.append("values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			// 设置动态参数的值
			int index = 1;
			pstmt.setString(index++, this.instCode);
			pstmt.setString(index++, this.orgaCode);
			pstmt.setString(index++, this.instName);
			pstmt.setString(index++, this.regCode);
			pstmt.setString(index++, this.areaCode);
			pstmt.setString(index++, this.subjection.getDataCode());
			pstmt.setString(index++, this.instGrade.getDataCode());
			pstmt.setString(index++, this.sbdd.getDataCode());
			pstmt.setString(index++, this.approve.getDataCode());
			pstmt.setString(index++, this.economy.getDataCode());
			pstmt.setString(index++, this.hygieneBig.getDataCode());
			pstmt.setString(index++, this.hygieneSmall.getDataCode());
			pstmt.setString(index++, this.unit);
			pstmt.setString(index++, this.openDate);
			pstmt.setString(index++, this.represent);
			pstmt.setDouble(index, this.funds);
			int count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtil.close(pstmt);
		}
	}

	public void edit() throws SQLException {
		Connection conn = null;
		try {
			conn = DBUtil.getConn();
			conn.setAutoCommit(false);
			editToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DBUtil.close(conn);
		}
	}

	private void editToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			String sql = "update medical set orgaCode=?,instName=?,regCode=?,areaCode=?,subjection=?,instGrade=?,sbdd=?,approve=?,economy=?,hygieneBig=?,hygieneSmall=?,unit=?,openDate=?,represent=?,funds=? where instCode=?";
			pstmt = conn.prepareStatement(sql);
			// 设置动态参数对应的值
			int index = 1;
			pstmt.setString(index++, this.orgaCode);
			pstmt.setString(index++, this.instName);
			pstmt.setString(index++, this.regCode);
			pstmt.setString(index++, this.areaCode);
			pstmt.setString(index++, this.subjection.getDataCode());
			pstmt.setString(index++, this.instGrade.getDataCode());
			pstmt.setString(index++, this.sbdd.getDataCode());
			pstmt.setString(index++, this.approve.getDataCode());
			pstmt.setString(index++, this.economy.getDataCode());
			pstmt.setString(index++, this.hygieneBig.getDataCode());
			pstmt.setString(index++, this.hygieneSmall.getDataCode());
			pstmt.setString(index++, this.unit);
			pstmt.setString(index++, this.openDate);
			pstmt.setString(index++, this.represent);
			pstmt.setDouble(index++, this.funds);
			pstmt.setString(index, this.instCode);
			int count = pstmt.executeUpdate();

		} catch (SQLException e) {
			throw e;
		} finally {
			DBUtil.close(pstmt);
		}

	}
	public void del(String instCode) throws SQLException {
		Connection conn = null;
		this.instCode = instCode;
		try {
			conn = DBUtil.getConn();
			conn.setAutoCommit(false);
			deleteFromDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DBUtil.close(conn);
		}

	}
	private void deleteFromDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"delete from medical where instCode=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.instCode);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("Failed to delete record from table !", e);
		} finally {
			DBUtil.close(pstmt);

		}
	}

	public String getInstCode() {
		return instCode;
	}

	public void setInstCode(String instCode) {
		this.instCode = instCode;
	}

	public String getOrgaCode() {
		return orgaCode;
	}

	public void setOrgaCode(String orgaCode) {
		this.orgaCode = orgaCode;
	}

	public String getInstName() {
		return instName;
	}

	public void setInstName(String instName) {
		this.instName = instName;
	}

	public String getRegCode() {
		return regCode;
	}

	public void setRegCode(String regCode) {
		this.regCode = regCode;
	}

	public Data getSubjection() {
		return subjection;
	}

	public void setSubjection(Data subjection) {
		this.subjection = subjection;
	}

	public Data getInstGrade() {
		return instGrade;
	}

	public void setInstGrade(Data instGrade) {
		this.instGrade = instGrade;
	}

	public Data getSbdd() {
		return sbdd;
	}

	public void setSbdd(Data sbdd) {
		this.sbdd = sbdd;
	}

	public Data getApprove() {
		return approve;
	}

	public void setApprove(Data approve) {
		this.approve = approve;
	}

	public Data getEconomy() {
		return economy;
	}

	public void setEconomy(Data economy) {
		this.economy = economy;
	}

	public Data getHygieneBig() {
		return hygieneBig;
	}

	public void setHygieneBig(Data hygieneBig) {
		this.hygieneBig = hygieneBig;
	}

	public Data getHygieneSmall() {
		return hygieneSmall;
	}

	public void setHygieneSmall(Data hygieneSmall) {
		this.hygieneSmall = hygieneSmall;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public String getOpenDate() {
		return openDate;
	}

	public void setOpenDate(String openDate) {
		this.openDate = openDate;
	}

	public String getRepresent() {
		return represent;
	}

	public void setRepresent(String represent) {
		this.represent = represent;
	}

	public String getAreaCode() {
		return areaCode;
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public Double getFunds() {
		return funds;
	}

	public void setFunds(double funds) {
		this.funds = funds;
	}

}
